package com.oazon.sonos.util;

import android.content.Context;

import com.oazon.moco.bean.AuthType;
import com.oazon.moco.bean.McServiceMsg;
import com.oazon.moco.bean.SessionIdAuthParams;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.sonos.core.AbsServiceHelper;

public class SessionIdHelper extends AbsServiceHelper {
	protected static McServiceMsg serviceMsg;
	
	public static McServiceMsg getServiceMsg(){
		return serviceMsg;
	}
	
	static {
		serviceMsg = new McServiceMsg<SessionIdAuthParams>();
		serviceMsg.setUri("http://douban.fm/partner/sonos/ws");
		serviceMsg.setName("豆瓣FM");
		serviceMsg.setAuthType(AuthType.SESSION_ID.value());
		serviceMsg.setSecureUri("https://www.douban.com/partner/sonos/ws");
		
		SessionIdAuthParams authParams = new SessionIdAuthParams();
		authParams.setSessionId("2691698:a985");
		serviceMsg.setAuthParams(authParams);
	}
	
	public SessionIdHelper(Context context,McServiceMsg serviceMsg) {
		this.context = context;
		this.url = serviceMsg.getUri();
		registMcServiceMsg(serviceMsg);
	/*	url = "http://douban.fm/partner/sonos/ws";
		sHeader = "<s:Header>"
				+ "<credentials xmlns=\"http://www.sonos.com/Services/1.1\">"
				+ "<deviceId>" + LinkSDK.UUID + "</deviceId>"
				+ "<deviceProvider>Sonos</deviceProvider>"
				+ "<sessionId>2691698:a985</sessionId>" + "</credentials>"
				+ "</s:Header>";*/
	}

	@Override
	public void registMcServiceMsg(McServiceMsg msg) {
		if (msg == null || !(msg.getAuthParams() instanceof SessionIdAuthParams)) {
			return;
		}
		String sessionid = ((SessionIdAuthParams) msg.getAuthParams())
				.getSessionId();
		if (sessionid == null) {
			sessionid = "2691698:a985";
		}
		sHeader = "<s:Header>"
				+ "<credentials xmlns=\"http://www.sonos.com/Services/1.1\">"
				+ "<deviceId>" + LinkSDK.UUID + "</deviceId>"
				+ "<deviceProvider>Sonos</deviceProvider>" + "<sessionId>"
				+ sessionid + "</sessionId>" + "</credentials>" + "</s:Header>";
	}

	@Override
	public McServiceMsg getMcServiceMsg() {
		return serviceMsg;
	}
}
